package com.vvlive.caibao.smartcity.callback;

import com.vvlive.caibao.smartcity.base.GetDeptInfo;
import com.vvlive.caibao.smartcity.bean.Dept;
import com.vvlive.caibao.smartcity.bean.MeetingRoom;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhj on 2015/11/9.
 * 加载机构和会议室的回掉函数
 */
public class LoadDeptRoomCallBack implements GetDeptInfo.OnLoadFinishListener {

    private Map<Dept, List<MeetingRoom>> mRoomInfo;
    private List<Dept> mDepts;
    private List<MeetingRoom> mMeetingRooms;
    private OnLoadListener mListener;

    public LoadDeptRoomCallBack() {
        mRoomInfo = new HashMap<>();
        mDepts = new ArrayList<>();
        mMeetingRooms = new ArrayList<>();
    }

    @Override
    public void onLoadFinish(List<Dept> depts, List<MeetingRoom> rooms) {
        mDepts = depts;
        mMeetingRooms = rooms;
        // make all departments and rooms a map
        for (Dept dept : depts) {
            List<MeetingRoom> meetingRooms = new ArrayList<>();
            for (MeetingRoom room : rooms) {
                if (dept.getDeptid().equals(room.getDeptid())) {
                    meetingRooms.add(room);
                }
            }
            mRoomInfo.put(dept, meetingRooms);
        }

        if (mListener != null) {
            mListener.onLoadFinish();
        }
    }

    @Override
    public void onLoadError(int errCode, String str) {
        if (mListener != null) {
            mListener.onLoadFailed(errCode, str);
        }
    }

    /**
     * 获得会议室信息，会议室和其所在的机构对应
     * @return
     */
    public Map<Dept, List<MeetingRoom>> getRoomInfo() {
        return mRoomInfo;
    }

    /**
     * 获得所有的机构
     * @return
     */
    public List<Dept> getDepts() {
        return mDepts;
    }

    /**
     * 获得所有的会议室，不分机构
     * @return
     */
    public List<MeetingRoom> getMeetingRooms() {
        return mMeetingRooms;
    }

    /**
     * 获取部门id数组
     *
     * @return
     */
    public String[] getDeptIds() {
        return getDeptInfoArray("deptid");
    }

    /**
     * 获取部门名称数组
     *
     * @return
     */
    public String[] getDeptNames() {
        return getDeptInfoArray("deptname");
    }

    private String[] getDeptInfoArray(String field) {
        int size = mRoomInfo.size();
        String[] fields = new String[size];
        Dept[] depts = getDeptArray(size);
        for (int i = 0; i < size; i++) {
            try {
                Dept dept = depts[i];
                fields[i] = String.valueOf(dept.getClass().getDeclaredMethod(getGetter(field)).invoke(dept));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return fields;
    }

    /**
     * 获取部门信息数组
     *
     * @return
     */
    private Dept[] getDeptArray(int size) {
        Object[] temp = mRoomInfo.keySet().toArray();
        Dept[] depts = new Dept[size];
        for (int i = 0; i < size; i++) {
            depts[i] = (Dept) temp[i];
        }
        return depts;
    }

    /**
     * 根据部门id获取会议室id数组
     *
     * @param deptId
     * @return
     */
    public String[] getRoomIdsByDeptId(String deptId) {
        return getRoomInfoArray(deptId, "roomid");
    }

    /**
     * 根据部门id获取会议室名称数组
     *
     * @param deptId
     * @return
     */
    public String[] getRoomBamesByDeptId(String deptId) {
        return getRoomInfoArray(deptId, "room");
    }

    private String[] getRoomInfoArray(String deptId, String field) {
        List<MeetingRoom> temp = getMeetingRoomsByDeptId(deptId);
        int size = temp.size();
        String[] fields = new String[size];
        for (int i = 0; i < size; i++) {
            try {
                MeetingRoom room = temp.get(i);
                fields[i] = String.valueOf(room.getClass().getDeclaredMethod(getGetter(field)).invoke(room));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return fields;
    }

    /**
     * 根据部门id获取会议室信息
     *
     * @param deptId
     * @return
     */
    public List<MeetingRoom> getMeetingRoomsByDeptId(String deptId) {
        for (Map.Entry<Dept, List<MeetingRoom>> entry : mRoomInfo.entrySet()) {
            if (deptId.equals(entry.getKey().getDeptid())) {
                return entry.getValue();
            }
        }
        return null;
    }

    /**
     *
     * @param deptId
     * @return
     */
    public MeetingRoom[] getMeetingRoomsArrayByDeptId(String deptId){
        List<MeetingRoom> roomList = getMeetingRoomsByDeptId(deptId);
        return getMeetingRoomsByDeptId(deptId).toArray(new MeetingRoom[roomList.size()]);
    }

    private String getGetter(String method){
        String firstCap = String.valueOf(method.charAt(0)).toUpperCase() + method.substring(1);
        return "get" + firstCap;
    }

    public interface OnLoadListener {

        void onLoadFinish();

        void onLoadFailed(int code, String msg);
    }

    public void setListener(OnLoadListener onLoadListener) {
        mListener = onLoadListener;
    }
}
